package com.module.dialog.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.module.dialog.ListDialog;
import com.module.dialog.R;
import com.module.dialog.inter.DialogOnItemListener;
import com.module.dialog.utils.ListType;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by dell on 2018/7/6
 */
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

    private Context mContext;
    private ListDialog.ListParam mListParam;
    private DialogOnItemListener onItemListener;

    public ListAdapter(Context context) {
        this.mContext = context;
    }

    public void setListParam(ListDialog.ListParam param) {
        this.mListParam = param;
    }

    public void setOnItemListener(DialogOnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View.inflate(mContext, R.layout.item_list, null);
        //解决item宽度不能铺满
        return new ListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder holder, final int position) {
        holder.tvText.setText(mListParam.getText(position));
        if (mListParam.type == ListType.Normal) {
        } else if (mListParam.type == ListType.Radio) {
            holder.icon.setSelected(position == mListParam.selectItem);
        } else if (mListParam.type == ListType.CheckBox) {
            holder.icon.setSelected(mListParam.checkItems[position]);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener != null) {
                    onItemListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mListParam.items.size();
    }

    public Drawable radioDrawable() {
        return drawableTintAndSelector(R.drawable.abc_btn_radio_n, R.drawable.abc_btn_radio_l);
    }

    public Drawable checkBoxDrawable() {
        return drawableTintAndSelector(R.drawable.abc_btn_check_n, R.drawable.abc_btn_check_l);
    }

    /**
     * 资源图片上色+选择器
     *
     * @return
     */
    public Drawable drawableTintAndSelector(int resId_n, int resId_l) {
        Drawable drawable_n = ContextCompat.getDrawable(mContext, resId_n).mutate();
        Drawable drawable_l = ContextCompat.getDrawable(mContext, resId_l).mutate();
        /** 着色tint */
        Drawable wrappedDrawable_n = DrawableCompat.wrap(drawable_n);
        //实际是蓝色，这里显示错误
        DrawableCompat.setTintList(wrappedDrawable_n, ColorStateList.valueOf(ContextCompat.getColor(mContext, R.color.dialog_icon)));
        Drawable wrappedDrawable_l = DrawableCompat.wrap(drawable_l);
        DrawableCompat.setTintList(wrappedDrawable_l, ColorStateList.valueOf(ContextCompat.getColor(mContext, R.color.dialog_icon)));
        /** 选择器selector */
        StateListDrawable selectorDrawable = new StateListDrawable();
        selectorDrawable.addState(new int[]{android.R.attr.state_selected}, wrappedDrawable_l);
        selectorDrawable.addState(new int[]{}, wrappedDrawable_n);
        return selectorDrawable;
    }

    class ListViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView tvText;

        public ListViewHolder(@NonNull View view) {
            super(view);
            icon = view.findViewById(R.id.icon);
            tvText = view.findViewById(R.id.text1);
            if (mListParam.type == ListType.Normal) {
                icon.setVisibility(View.GONE);
                tvText.setGravity(Gravity.CENTER);
            } else if (mListParam.type == ListType.Radio) {
                icon.setVisibility(View.VISIBLE);
                icon.setImageDrawable(radioDrawable());
            } else if (mListParam.type == ListType.CheckBox) {
                icon.setVisibility(View.VISIBLE);
                icon.setImageDrawable(checkBoxDrawable());
            }
        }
    }


}